package com.example.iorder.reservation;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.domain.Menu;
import com.example.domain.OrderedMenuItem;
import com.example.iorder.R;
import com.example.iorder.util.TextFormatter;

public class ViewOrderedMenuItems extends Activity {
	private MenuListAdapter adapter;
	private TextView priceView;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.reservation_view_ordered_menu_items);
		
		priceView = (TextView) findViewById(R.id.rTotalPrice);
		View viewWithId = findViewById(R.id.menuListview);
		ExpandableListView listView = (ExpandableListView) viewWithId;
		
		// Get the ordered menu items
		Bundle orderedMenuItemsInfo = getIntent().getExtras();
		ArrayList<Menu> menus = (ArrayList<Menu>) orderedMenuItemsInfo.getSerializable("menus");
		ArrayList<ArrayList<OrderedMenuItem>> menuItems = (ArrayList<ArrayList<OrderedMenuItem>>) orderedMenuItemsInfo.getSerializable("menuItems");
		
		// Add the items to the adapter
		adapter = new MenuListAdapter(this, menus, menuItems);
        listView.setAdapter(adapter);
        
        updateTotalPrice();
	}
	
	public void reduceAmountOfSelectedItem(View view) {
		ImageButton clickedButton = (ImageButton) view;
		View parentView = (View) clickedButton.getParent();
		if (parentView instanceof RelativeLayout) {
			int menuId = parentView.getTag(R.id.menuID) != null ? (Integer) parentView.getTag(R.id.menuID) : -1;
			int menuItemId = parentView.getTag(R.id.menuItemID) != null ? (Integer) parentView.getTag(R.id.menuItemID) : -1;
			
			// Retrieval of menu id and menu item id was successful
			if (menuId != -1 && menuItemId != -1) {
				adapter.decreaseAmountOfOrderedMenuItem(menuId, menuItemId);
			}
			
			// Update the total price
			updateTotalPrice();
		} else
			Toast.makeText(this, "An error occured while attempting to remove the selected item", Toast.LENGTH_SHORT).show();
	}
	
	public void increaseAmountOfSelectedItem(View view) {
		ImageButton clickedButton = (ImageButton) view;
		View parentView = (View) clickedButton.getParent();
		if (parentView instanceof RelativeLayout) {
			int menuId = parentView.getTag(R.id.menuID) != null ? (Integer) parentView.getTag(R.id.menuID) : -1;
			int menuItemId = parentView.getTag(R.id.menuItemID) != null ? (Integer) parentView.getTag(R.id.menuItemID) : -1;
			
			// Retrieval of menu id and menu item id was successful
			if (menuId != -1 && menuItemId != -1) {
				adapter.increaseAmountOfOrderedMenuItem(menuId, menuItemId);
			}
			
			// Update the total price
			updateTotalPrice();
		} else
			Toast.makeText(this, "An error occured while attempting to add the selected item", Toast.LENGTH_SHORT).show();
	}
	
	private void updateTotalPrice() {
		double newTotalPrice = adapter.getCurrentTotalPrice();
		String formattedPrice = TextFormatter.formatStringForTotalPrice(this, newTotalPrice);
		priceView.setText(formattedPrice);
	}
	
	public ArrayList<ArrayList<OrderedMenuItem>> getListOfMenuItems() {
		return adapter.getAllChildren();
	}
}
